package com.xd.spring.liteflow.component;

import cn.hutool.core.util.RandomUtil;
import com.xd.spring.liteflow.entity.UserInfo;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component("b")
@Slf4j
public class BCmp extends NodeComponent {
    @Override
    public void process() throws Exception {
        String name = Thread.currentThread().getName();
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StackTraceElement stackTraceElement = stackTraceElements[1];
        String className = stackTraceElement.getClassName();
        String methodName = stackTraceElement.getMethodName();
        int lineNumber = stackTraceElement.getLineNumber();
        log.info("{} {} {} {} {} begin", name, className, methodName, lineNumber, System.currentTimeMillis());
        TimeUnit.SECONDS.sleep(2);
        UserInfo userInfo = this.getContextBean(UserInfo.class);
        log.info("{}", ToStringBuilder.reflectionToString(userInfo, ToStringStyle.JSON_STYLE));
        if (Objects.nonNull(userInfo)) {
            if (Objects.isNull(userInfo.getName())) {
                userInfo.setName("zhang" + RandomUtil.randomNumbers(1));
            }
        }
        log.info("{}", ToStringBuilder.reflectionToString(userInfo, ToStringStyle.JSON_STYLE));
        log.info("{} {} {} {} {} end", name, className, methodName, lineNumber, System.currentTimeMillis());
    }
}
